package com.momo.service;

import com.google.inject.Inject;
import com.momo.res.JSONResult;
import com.momo.res.WXresult;
import com.momo.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.bean.menu.WxMenuRule;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInRedisConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Kagome on 2019/5/15.
 */
@Service
@Slf4j
public class WxMenuService {
    private WxMpService wxService;
    @Autowired
    private  RedisTemplate<String, String> redisTemplateString;
    @Autowired
    private RedisUtil redisUtil;

    public WxMenuService() {

    }
    @Autowired
    public WxMenuService(WxMpService wxService,RedisTemplate<String, String> redisTemplateString) {
        this.wxService = wxService;
        this.redisTemplateString=redisTemplateString;
    }

    public JSONResult menuCreate() throws Exception{
        try {
            WxMenu menu = new WxMenu();
            WxMenuButton button1 = new WxMenuButton();
            button1.setType(WxConsts.MenuButtonType.CLICK);
            button1.setName("今日歌曲");
            button1.setKey("V1001_TODAY_MUSIC");

//        WxMenuButton button2 = new WxMenuButton();
//        button2.setType(WxConsts.BUTTON_MINIPROGRAM);
//        button2.setName("小程序");
//        button2.setAppId("wx286b93c14bbf93aa");
//        button2.setPagePath("pages/lunar/index.html");
//        button2.setUrl("http://mp.weixin.qq.com");

            WxMenuButton button3 = new WxMenuButton();
            button3.setName("菜单");

            menu.getButtons().add(button1);
//        menu.getButtons().add(button2);
            menu.getButtons().add(button3);

            WxMenuButton button31 = new WxMenuButton();
            button31.setType(WxConsts.MenuButtonType.VIEW);
            button31.setName("搜索");
            button31.setUrl("http://www.soso.com/");

            WxMenuButton button32 = new WxMenuButton();
            button32.setType(WxConsts.MenuButtonType.VIEW);
            button32.setName("视频");
            button32.setUrl("http://v.qq.com/");

            WxMenuButton button33 = new WxMenuButton();
            button33.setType(WxConsts.MenuButtonType.CLICK);
            button33.setName("赞一下我们");
            button33.setKey("V1001_GOOD");

            WxMenuButton button34 = new WxMenuButton();
            button34.setType(WxConsts.MenuButtonType.VIEW);
            button34.setName("获取用户信息");

            ServletRequestAttributes servletRequestAttributes =
                    (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            if (servletRequestAttributes != null) {
                HttpServletRequest request = servletRequestAttributes.getRequest();

                URL requestURL = new URL(request.getRequestURL().toString());
                String url = this.wxService.oauth2buildAuthorizationUrl(
                        String.format("%s://%s/wx/redirect/greet", requestURL.getProtocol(), requestURL.getHost()),
                        WxConsts.OAuth2Scope.SNSAPI_USERINFO, null);
                button34.setUrl(url);
            }

            button3.getSubButtons().add(button31);
            button3.getSubButtons().add(button32);
            button3.getSubButtons().add(button33);
            button3.getSubButtons().add(button34);
            // 设置菜单
            String str = wxService.getMenuService().menuCreate(menu);
            return WXresult.jsonResult(str);
        } catch (WxErrorException e) {
            e.getMessage();
            log.error("设置微信菜单异常:{} , {} , {}", e.getMessage(), e.getError(), e);
            return JSONResult.errorMap("设置微信菜单异常");
        }

    }

}
